package org.lq.er.view.score;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.lq.er.entity.Student;
import org.lq.er.service.StudentService;
import org.lq.er.service.impl.StudentServiceImpl;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
/**
 * 成绩查询校验视图
 * @author 宋铀
 * 
 * @package org.lq.er.view.score
 * @date 2020年8月22日
 */
public class ER_score_check extends JFrame {

	private JPanel contentPane;
	private JTextField text_cardId;
	private JTextField text_passwd;
	private JButton btn_search;
	private StudentService ss = new StudentServiceImpl(); 

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ER_score_check frame = new ER_score_check();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ER_score_check() {
		setTitle("查询成绩");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 351, 219);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.white);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lbl_cardId = new JLabel(" 身份证号:");
		lbl_cardId.setBounds(62, 53, 73, 15);
		panel.add(lbl_cardId);
		
		JLabel lbl_password = new JLabel("密 码:");
		lbl_password.setBounds(89, 94, 46, 15);
		panel.add(lbl_password);
		
		text_cardId = new JTextField();
		text_cardId.setBounds(145, 50, 110, 21);
		panel.add(text_cardId);
		text_cardId.setColumns(10);
		
		text_passwd = new JTextField();
		text_passwd.setBounds(145, 91, 110, 21);
		panel.add(text_passwd);
		text_passwd.setColumns(10);
		
		btn_search = new JButton("查询成绩");
		btn_search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 获取组件中的值
				String cardId = text_cardId.getText();
				String passwd = text_passwd.getText();
				Student student = ss.check(cardId, passwd);
				if (student != null) {
					// 创建成绩视图
					ER_score erscore = new ER_score(student);
					erscore.setBounds(ER_score_check.this.getX()+50, ER_score_check.this.getY()+20, erscore.getWidth(), erscore.getHeight());
					erscore.setVisible(true);
					
					// 监听窗体是否被关闭
					erscore.addWindowListener(new WindowAdapter() {
						@Override
						public void windowClosed(WindowEvent e) {
							//当添加窗体关闭的时候,调用表格初始化方法
							initText();
						}
					});
					
				} else {
					JOptionPane.showMessageDialog(ER_score_check.this, "身份证号或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
				}
				
			}
		});
		btn_search.setBounds(120, 126, 93, 23);
		panel.add(btn_search);
		
	}
	
	/**
	 * 初始化文本内容
	 */
	public void initText() {
		text_cardId.setText("");
		text_passwd.setText("");
	}
}
